package com.example.springboottest.student.service.impl;

import com.example.springboottest.student.model.Student;
import com.example.springboottest.student.service.StuPdfExportService;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.List;
import java.util.Map;

/**
 * 中文显示有问题
 */
@Service("stuPdfExportService")
public class StuPdfExportServiceImpl implements StuPdfExportService {
    @Override
    public void make(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) {
        try {
            BaseFont bfComic = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//支持中文
            // A4纸张
            document.setPageSize(PageSize.A4);
            // 标题
            document.addTitle("用户信息");
            // 换行
            document.add(new Chunk("\n"));
            // 表格，3列
            PdfPTable table = new PdfPTable(4);
            // 单元格
            PdfPCell cell = null;
            // 字体，定义为蓝色加粗
            Font f10 = new Font(bfComic, 10, Font.NORMAL);
            f10.setColor(Color.BLUE);
            f10.setStyle(Font.BOLD);
            // 字体，
            Font f8 = new Font(bfComic, 8, Font.NORMAL);

            // 标题
            cell = new PdfPCell(new Paragraph("ID", f10));
            // 居中对齐ID
            cell.setHorizontalAlignment(1);
            // 将单元格加入表格
            table.addCell(cell);

            // 标题
            cell = new PdfPCell(new Paragraph("年龄", f10));
            // 居中对齐
            cell.setHorizontalAlignment(1);
            // 将单元格加入表格
            table.addCell(cell);

            // 标题
            cell = new PdfPCell(new Paragraph("姓名", f10));
            // 居中对齐
            cell.setHorizontalAlignment(1);
            // 将单元格加入表格
            table.addCell(cell);

            // 标题
            cell = new PdfPCell(new Paragraph("地址", f10));
            // 居中对齐
            cell.setHorizontalAlignment(1);
            // 将单元格加入表格
            table.addCell(cell);

            // 获取数据模型中的manList
            java.util.List<Student> manList = (List<Student>) model.get("list");
            for (Student man : manList) {
                document.add(new Chunk("\n"));
                cell = new PdfPCell(new Paragraph(man.getId() + "", f8));
                // 居中对齐
                cell.setHorizontalAlignment(1);
                table.addCell(cell);
                cell = new PdfPCell(new Paragraph(man.getAge() + "", f8));
                // 居中对齐
                cell.setHorizontalAlignment(1);
                table.addCell(cell);
                cell = new PdfPCell(new Paragraph(man.getName(), f8));
                // 居中对齐
                cell.setHorizontalAlignment(1);
                table.addCell(cell);
                cell = new PdfPCell(new Paragraph(man.getAddress(), f8));
                // 居中对齐
                cell.setHorizontalAlignment(1);
                table.addCell(cell);
            }
            // 文档中加入表格
            document.add(table);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
